<?php
/**
 * This Test creates an order with 3 OrdersProducts, 
 * payed in Euros instead of the standard Dollars
 */
require_once(TEST_INTEGRATION_PATH . 'OrderFactory.php');
require_once(TEST_DTO_PATH.'ProductDTO.php');
require_once(TEST_DTO_PATH.'ProductOptionDTO.php');
require_once(TEST_DTO_PATH.'OrderDTO.php');

class TestOfOrderWithMultipleProducts extends UnitTestCase {
	private $db;
	private $order;
	private $productDTO;
	private $productOptionDTO;
	private $orderDTO;
	
	function __construct() {
		$this->db = new Datasource(TEST_DB_SERVER, TEST_DB_DATABASE, TEST_DB_SERVER_USERNAME, TEST_DB_SERVER_PASSWORD);
		
		$this->createSchema();
		
		$p1= new ProductDTO;
		$p1->pid = 11;
		
		$p2 = new ProductDTO;
		$p2->pid = 13;
		
		$p3 = new ProductDTO;
		$p3->pid = 17;
		
		$cashierDTO = new CashierDTO;
		$cashierDTO->name = "John";
		
		$this->orderDTO = new OrderDTO();
		$this->orderDTO->products = array($p1,$p2,$p3);
		$this->orderDTO->price = 104.97;
		$this->orderDTO->currency = "EUR";
		$this->orderDTO->method = "cod";
		$this->orderDTO->cashier = $cashierDTO;
		
		$this->order = new OrderFactory();
	}
	
	private function createSchema() {
		/* This order has been made via the Zen Cart Front-End to have a direct comparisson of values */
		$this->db->execute("INSERT INTO `orders` (`orders_id` ,`customers_id` ,`customers_name` ,`customers_company` ,`customers_street_address` ,`customers_suburb` ,`customers_city` ,`customers_postcode` ,`customers_state` ,`customers_country` ,`customers_telephone` ,`customers_email_address` ,`customers_address_format_id` ,`delivery_name` ,`delivery_company` ,`delivery_street_address` ,`delivery_suburb` ,`delivery_city` ,`delivery_postcode` ,`delivery_state` ,`delivery_country` ,`delivery_address_format_id` ,`billing_name` ,`billing_company` ,`billing_street_address` ,`billing_suburb` ,`billing_city` ,`billing_postcode` ,`billing_state` ,`billing_country` ,`billing_address_format_id` ,`payment_method` ,`payment_module_code` ,`shipping_method` ,`shipping_module_code` ,`coupon_code` ,`cc_type` ,`cc_owner` ,`cc_number` ,`cc_expires` ,`last_modified` ,`date_purchased` ,`orders_status` ,`orders_date_finished` ,`currency` ,`currency_value` ,`order_total` ,`order_tax` ,`paypal_ipn_id` ,`ip_address`) VALUES ('3', '1', 'Bill Smith', 'JustaDemo', '123 Any Avenue', '', 'Here', '12345', 'California', 'United States', '12345', 'root@localhost', '2', 'Bill Smith', 'JustaDemo', '123 Any Avenue', '', 'Here', '12345', 'California', 'United States', '2', 'Bill Smith', 'JustaDemo', '123 Any Avenue', '', 'Here', '12345', 'California', 'United States', '2', 'Cash on Delivery', 'cod', 'Store Pickup (Walk In)', 'storepickup', '', '', '', '', '', NULL , '2008-02-22 20:44:00', '3', NULL , 'EUR', '0.741300', '104.97', '0.00', '0', '127.0.0.1 - 127.0.0.1')");
		$this->db->execute("INSERT INTO `orders_products` (`orders_products_id` ,`orders_id` ,`products_id` ,`products_model` ,`products_name` ,`products_price` ,`final_price` ,`products_tax` ,`products_quantity` ,`onetime_charges` ,`products_priced_by_attribute` ,`product_is_free` ,`products_discount_type` ,`products_discount_type_from` ,`products_prid`) VALUES ('8', '3', '13', 'DVD-LTWP', 'Lethal Weapon Linked', '34.9900', '34.9900', '0.0000', '1', '0.0000', '0', '0', '0', '0', '13')");
		$this->db->execute("INSERT INTO `orders_products` (`orders_products_id` ,`orders_id` ,`products_id` ,`products_model` ,`products_name` ,`products_price` ,`final_price` ,`products_tax` ,`products_quantity` ,`onetime_charges` ,`products_priced_by_attribute` ,`product_is_free` ,`products_discount_type` ,`products_discount_type_from` ,`products_prid`)VALUES ('9', '3', '11', 'DVD-FDBL', 'Fire Down Below Linked', '29.9900', '29.9900', '0.0000', '1', '0.0000', '0', '0', '0', '0', '11')");
		$this->db->execute("INSERT INTO `orders_products` (`orders_products_id` ,`orders_id` ,`products_id` ,`products_model` ,`products_name` ,`products_price` ,`final_price` ,`products_tax` ,`products_quantity` ,`onetime_charges` ,`products_priced_by_attribute` ,`product_is_free` ,`products_discount_type` ,`products_discount_type_from` ,`products_prid`) VALUES ('10', '3', '17', 'DVD-SPEED', 'Speed Linked', '39.9900', '39.9900', '0.0000', '1', '0.0000', '0', '0', '0', '0', '17')");
		$this->db->execute("INSERT INTO `orders_status_history` (`orders_status_history_id` ,`orders_id` ,`orders_status_id` ,`date_added` ,`customer_notified` ,`comments`) VALUES ('5', '3', '1', '2008-02-22 20:44:00', '3', '')");
		$this->db->execute("INSERT INTO `orders_total` (`orders_total_id` ,`orders_id` ,`title` ,`text` ,`value` ,`class` ,`sort_order`) VALUES ('12', '3', 'Total:', '&euro;104.97', '104.9700', 'ot_total', '999')");		
	}
	
	private function dropSchema() {
		$this->db->execute("DELETE FROM `orders` WHERE `orders_id` = 3");
		$this->db->execute("DELETE FROM `orders` WHERE `orders_id` = 4");
		$this->db->execute("DELETE FROM `orders_products` WHERE `orders_id` = 3");
		$this->db->execute("DELETE FROM `orders_products` WHERE `orders_id` = 4");
		$this->db->execute("DELETE FROM `orders_status_history` WHERE `orders_id` = 3");
		$this->db->execute("DELETE FROM `orders_status_history` WHERE `orders_id` = 4");
		$this->db->execute("DELETE FROM `orders_total` WHERE `orders_id` = 3");
		$this->db->execute("DELETE FROM `orders_total` WHERE `orders_id` = 4");
	}
	
	function testOrderExecute() {
		$this->assertTrue($this->order->execute($this->orderDTO));
	}

	function testOrdersTable() {
		// comparing all fields except the orders_id, date_purchased, and ip_address
		$originalorder =	$this->db->execute(" SELECT `customers_id` , `customers_name` , `customers_company` , `customers_street_address` , `customers_suburb` , `customers_city` , `customers_postcode` , `customers_state` , `customers_country` , `customers_telephone` , `customers_email_address` , `customers_address_format_id` , `delivery_name` , `delivery_company` , `delivery_street_address` , `delivery_suburb` , `delivery_city` , `delivery_postcode` , `delivery_state` , `delivery_country` , `delivery_address_format_id` , `billing_name` , `billing_company` , `billing_street_address` , `billing_suburb` , `billing_city` , `billing_postcode` , `billing_state` , `billing_country` , `billing_address_format_id` , `payment_method` , `payment_module_code` , `shipping_method` , `shipping_module_code` , `coupon_code` , `cc_type` , `cc_owner` , `cc_number` , `cc_expires` , `cc_cvv` , `last_modified` , `orders_status` , `orders_date_finished` , `currency` , `currency_value` , `order_total` , `order_tax` , `paypal_ipn_id` FROM `orders` WHERE `orders_id` =3");
		$neworder =			$this->db->execute(" SELECT `customers_id` , `customers_name` , `customers_company` , `customers_street_address` , `customers_suburb` , `customers_city` , `customers_postcode` , `customers_state` , `customers_country` , `customers_telephone` , `customers_email_address` , `customers_address_format_id` , `delivery_name` , `delivery_company` , `delivery_street_address` , `delivery_suburb` , `delivery_city` , `delivery_postcode` , `delivery_state` , `delivery_country` , `delivery_address_format_id` , `billing_name` , `billing_company` , `billing_street_address` , `billing_suburb` , `billing_city` , `billing_postcode` , `billing_state` , `billing_country` , `billing_address_format_id` , `payment_method` , `payment_module_code` , `shipping_method` , `shipping_module_code` , `coupon_code` , `cc_type` , `cc_owner` , `cc_number` , `cc_expires` , `cc_cvv` , `last_modified` , `orders_status` , `orders_date_finished` , `currency` , `currency_value` , `order_total` , `order_tax` , `paypal_ipn_id` FROM `orders` WHERE `orders_id` =4");
		$this->assertEqual(mysql_fetch_array($originalorder), mysql_fetch_array($neworder));
	}
	
	function testOrdersProductsTable() {
		// comparing all fields except orders_id and products_prid
		// this should have three rows corresponding to the 3 ordered products
		$originalorder =	$this->db->execute("SELECT `products_id` , `products_model` , `products_name` , `products_price` , `final_price` , `products_tax` , `products_quantity` , `onetime_charges` , `products_priced_by_attribute` , `product_is_free` , `products_discount_type` , `products_discount_type_from` , `products_prid` FROM `orders_products` WHERE `orders_id` =3");
		$neworder =			$this->db->execute("SELECT `products_id` , `products_model` , `products_name` , `products_price` , `final_price` , `products_tax` , `products_quantity` , `onetime_charges` , `products_priced_by_attribute` , `product_is_free` , `products_discount_type` , `products_discount_type_from` , `products_prid` FROM `orders_products` WHERE `orders_id` =4");
		$this->assertEqual($this->db->nextRow($originalorder), $this->db->nextRow($neworder));
		$this->assertEqual($this->db->nextRow($originalorder), $this->db->nextRow($neworder));
		$this->assertEqual($this->db->nextRow($originalorder), $this->db->nextRow($neworder));
	}
	
	function testOrdersTotalTable() {
		// comparing only ot_total without the fields orders_total_id and orders_id
		$originalorder =	$this->db->execute("SELECT `title` , `text` , `value` , `class` , `sort_order` FROM `orders_total` WHERE `orders_id` =3 AND `class` LIKE 'ot_total' LIMIT 0 , 1 ");
		$neworder = 		$this->db->execute("SELECT `title` , `text` , `value` , `class` , `sort_order` FROM `orders_total` WHERE `orders_id` =4 AND `class` LIKE 'ot_total' LIMIT 0 , 1 ");
		$this->assertEqual(mysql_fetch_array($originalorder), mysql_fetch_array($neworder));
	}
	
	function testOrdersProductsAttributesTable() {
		$originalorder =	$this->db->execute("SELECT `products_options` , `products_options_values` , `options_values_price` , `price_prefix` , `product_attribute_is_free` , `products_attributes_weight` , `products_attributes_weight_prefix` , `attributes_discounted` , `attributes_price_base_included` , `attributes_price_onetime` , `attributes_price_factor` , `attributes_price_factor_offset` , `attributes_price_factor_onetime` , `attributes_price_factor_onetime_offset` , `attributes_qty_prices` , `attributes_qty_prices_onetime` , `attributes_price_words` , `attributes_price_words_free` , `attributes_price_letters` , `attributes_price_letters_free` , `products_options_id` , `products_options_values_id` FROM `orders_products_attributes` WHERE `orders_id` =3");
		$neworder = 		$this->db->execute("SELECT `products_options` , `products_options_values` , `options_values_price` , `price_prefix` , `product_attribute_is_free` , `products_attributes_weight` , `products_attributes_weight_prefix` , `attributes_discounted` , `attributes_price_base_included` , `attributes_price_onetime` , `attributes_price_factor` , `attributes_price_factor_offset` , `attributes_price_factor_onetime` , `attributes_price_factor_onetime_offset` , `attributes_qty_prices` , `attributes_qty_prices_onetime` , `attributes_price_words` , `attributes_price_words_free` , `attributes_price_letters` , `attributes_price_letters_free` , `products_options_id` , `products_options_values_id` FROM `orders_products_attributes` WHERE `orders_id` =4");
		$this->assertEqual(mysql_fetch_array($originalorder), mysql_fetch_array($neworder));
	}
	
	function testOrdersProductsQuantityReduction() {
		// Each product has 10 in the inventory to start
		$newquantityProduct10 = $this->db->execute("SELECT `products_quantity` FROM `products` WHERE products_id =10");
		$this->assertEqual(9, (int)mysql_result($newquantityProduct10, "products_quantity"));
		
		$newquantityProduct13 = $this->db->execute("SELECT `products_quantity` FROM `products` WHERE products_id =13");
		$this->assertEqual(9, (int)mysql_result($newquantityProduct13, "products_quantity"));
		
		$newquantityProduct17 = $this->db->execute("SELECT `products_quantity` FROM `products` WHERE products_id =17");
		$this->assertEqual(9, (int)mysql_result($newquantityProduct17, "products_quantity"));
		//$this->dropSchema();
	}
}

?>
